package rj.servlet.sy4;



import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

public class OrderServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;

    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=utf-8");
        request.setCharacterEncoding("utf-8");
        String foodName[] = request.getParameterValues("choice");
        if (foodName!= null) {
            for (int k = 0; k < foodName.length; k++) {
                request.setAttribute(foodName[k], foodName[k]);
                // 应该改为将食物存储到 HttpSession 中
                HttpSession session = request.getSession();
                session.setAttribute(foodName[k], foodName[k]);
            }
        }
        String url = "<A href='CartServlet'> 欢迎去购物车页面！</A>";
        response.getWriter().print("<P>点击超链接，链接到购物车页面,去查看购物车中的商品。<br />" + url);
    }

    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=utf-8");
        request.setCharacterEncoding("utf-8");
        // TODO Auto-generated method stub
        doGet(request, response);
    }
}

